package com.example.shoppingmallclient.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.example.shoppingmallclient.R;
import com.example.shoppingmallclient.vo.OrderItemVo;
import com.example.shoppingmallclient.vo.ProductListVo;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 袁梦达 2019012364
 */

@SuppressWarnings("all")
public class OrderItemListAdapter extends BaseAdapter {

    private List<OrderItemVo> productList = new ArrayList<>();
    private int layoutResId;
    private Context context;

    private ImageView ivPic;
    private TextView tvBaseInformation;
    private TextView tvPrice;
    private TextView tvQuantity;
    private TextView tvSumPrice;

    public OrderItemListAdapter(List<OrderItemVo> productList, int layoutResId, Context context) {
        this.productList = productList;
        this.layoutResId = layoutResId;
        this.context = context;
    }

    @Override
    public int getCount() {
        if(productList != null){
            return productList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if(productList != null){
            return productList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if(convertView == null){
            //加载item布局文件
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(layoutResId, null, false);
        }

        //在布局文件中显示数据

        //获取控件
        ivPic = convertView.findViewById(R.id.iv_pic);
        tvBaseInformation = convertView.findViewById(R.id.tv_base_information);
        tvPrice = convertView.findViewById(R.id.tv_price);
        tvQuantity = convertView.findViewById(R.id.tv_quantity);
        tvSumPrice = convertView.findViewById(R.id.tv_sum_price);


        //设置控件显示内容
        ivPic.setImageBitmap(productList.get(position).getImage());
        tvBaseInformation.setText(productList.get(position).getBaseInformation());
        tvPrice.setText("单价：" + productList.get(position).getPrice());
        tvQuantity.setText("数量" + productList.get(position).getQuantity());
        tvSumPrice.setText("总价：" + productList.get(position).getSumPrice());

        return convertView;
    }

}
